<?php


namespace App\Http\Controllers\Admin\Czy;


use App\Common\Responses;
use App\Http\Controllers\Admin\BaseController;
use App\Utils\Adapter\Map;
use App\Utils\Service\SmsSender;
use App\Utils\Thirdparty\Map\Amap;
use App\Utils\Thirdparty\Map\BaiduMap;
use App\Utils\Thirdparty\Map\Wechat;
use Illuminate\Http\Request;

class TestCzyController extends BaseController
{

    /**
     * 获取定位
     */
    public function getIp(Request $request)
    {
        try {
            $ip = $request->ip();
            //适配器模式调用 和策略模式相仿
            $amapAdapter = new Map(new BaiduMap());
            $result = $amapAdapter->position($ip);
            return $this->success($result, 0, $ip);
        } catch (\Exception $exception) {
            return $this->error($exception->getCode(), $exception->getMessage());
        }
    }

    /**
     * 步行规划
     * @param Request $request
     * @return false|string
     */
    public function getWalkRout(Request $request)
    {
        try {

            $params = $request->validate([
                'origin' => 'required',
                'destination' => 'required'
            ]);
            //通过获取 开始和结束地址的经纬度  执行规划
            $amapAdapter = new Map(new Wechat());
            $start = $amapAdapter->getLocation($params['origin']);
            $end   = $amapAdapter->getLocation($params['destination']);
            $result =$amapAdapter->walkRouting($start,$end);
            return Responses::success($result);
        } catch (\Exception $exception) {

            return $this->error($exception->getCode(), $exception->getMessage());
        }

    }

    /**
     * 公交规划
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getBusRout(Request $request)
    {
        try {
            //高德
//            $params = $request->validate([
//                'origin' => 'required',
//                'destination' => 'required',
//                'city'=>'required',
//            ]);
//            $amapAdapter = new Map(new BaiduMap());
//            $start = $amapAdapter->getLocation($params['origin']);
//            $end   = $amapAdapter->getLocation($params['destination']);
//            $result =$amapAdapter->busRouting($start,$end,$params['city']);

            //百度  。。腾讯
            $params = $request->validate([
                'origin' => 'required',
                'destination' => 'required',
            ]);
            $amapAdapter = new Map(new Wechat());
            $start = $amapAdapter->getLocation($params['origin']);
            $end   = $amapAdapter->getLocation($params['destination']);
            $result =$amapAdapter->busRouting($start,$end);

            return $this->success($result, 200);
        } catch (\Exception $exception) {

            return $this->error($exception->getCode(), $exception->getMessage());
        }

    }

    /**
     * 驾车规划
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getDrivingRouting(Request $request)
    {
        try {

//            //百度 高德
//            $params = $request->validate([
//                'origin' => 'required',
//                'destination' => 'required',
//            ]);
//            //获取开始地址和
//            $amapAdapter = new Map(new BaiduMap());
//            $start = $amapAdapter->getLocation($params['origin']);
//            $end   = $amapAdapter->getLocation($params['destination']);
//            $result =$amapAdapter->drivingRouting($start,$end);

            //腾讯   waypoints  途径地 （可选）
            $params = $request->validate([
                'origin' => 'required',
                'destination' => 'required',
                'waypoints' =>"required"
            ]);
            $amapAdapter = new Map(new Wechat());
            $start = $amapAdapter->getLocation($params['origin']);
            $end   = $amapAdapter->getLocation($params['destination']);
            $waypoints= $amapAdapter->getLocation($params['waypoints']);
            $result =$amapAdapter->drivingRouting($start,$end,$waypoints);

            return $this->success($result, 200);
        } catch (\Exception $exception) {

            return $this->error($exception->getCode(), $exception->getMessage());
        }

    }

    /**
     * 骑行规划
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getBicyclingRouting (Request $request)
    {
        try {

           $params = $request->validate([
                'origin' => 'required',
                'destination' => 'required',

            ]);
            $amapAdapter = new Map(new Wechat());
            $start = $amapAdapter->getLocation($params['origin']);
            $end   = $amapAdapter->getLocation($params['destination']);
            $result =$amapAdapter->bicyclingRouting($start,$end);
            return $this->success($result, 200);
        } catch (\Exception $exception) {
            return $this->error($exception->getCode(), $exception->getMessage());
        }

    }


    /**
     * 距离测量
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getDistance (Request $request)
    {
        try {
            //高德  腾讯
            $params = $request->validate([
                'origin' => 'required',
                'destination' => 'required',
            ]);
            //获取开始地址和
            $amapAdapter = new Map(new Amap());
            $start = $amapAdapter->getLocation($params['origin']);
            $end   = $amapAdapter->getLocation($params['destination']);
            $result =$amapAdapter->distance($start,$end,'driving');
            return $this->success($result, 200);
        } catch (\Exception $exception) {
            return $this->error($exception->getCode(), $exception->getMessage());
        }

    }

    /**
     * (未测试)
     * 测试短信发送 ritberMq
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getRabbitMQ(Request $request)
    {
        $smsSender = new SmsSender();
        $smsSender->send($request->get('message'));

    }

}
